<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src='../js/vue.js'></script>
</head>

<body>
    <div class="class">
        <input type="text" v-model="person">
        <button @click="sort =0">原序</button>
        <button @click="sort =1">升序</button>
        <button @click="sort =2">降序</button>
        <ul>
            <li v-for="(item,index) in filepersons" :key="item.id">
                {{item.name}}-{{item.age}}
            </li>
        </ul>
    </div>
    <script>
        const vm = new Vue({
            el: '.class',
            data: {
                person: '',
                sort: 0,
                persons: [
                    { id: '001', name: '马冬梅', sex: '女', age: 23 },
                    { id: '002', name: '周冬雨', sex: '女', age: 56 },
                    { id: '003', name: '周杰伦', sex: '男', age: 34 },
                    { id: '004', name: '扁嘴伦', sex: '男', age: 31 }
                ]
            },
            //筛选
            computed: {
                filepersons() {
                    const arr = this.persons.filter(item => {
                        return item.name.includes(this.person)
                    })
                    //排序

                    if (this.sort) {
                        console.log(666);
                        
                        arr.sort((a, b) => {
                            return this.sort === 1 ? a.age - b.age:b.age - a.age
                        })
                    }
                    return arr
                }
            }
        })
    </script>
</body>

</html>